package com.ken.checkviolationapp.ui.news

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.cimcitech.tebaoscreen.base.BaseRecyclerViewAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.ken.base_http_library.news.NewsResponse
import com.ken.base_library.ui.BaseFragment
import com.ken.checkviolationapp.R
import com.ken.checkviolationapp.adapter.NewsViewPage2Adapter
import com.ken.checkviolationapp.bean.ANewsBean
import com.ken.checkviolationapp.bean.BNewsBean
import com.ken.checkviolationapp.bean.TypeInterf
import com.ken.checkviolationapp.databinding.FragmentNewsBinding
import com.ken.checkviolationapp.utils.DepthPageTransformer
import com.ken.rxhttp.callback.OnDesCallback
import com.ken.rxhttp.http_api_impl.ApiServiceImpl

class NewsFragment : BaseFragment() {
    val TAG = NewsFragment::class.java.name
    private lateinit var newsViewModel: NewsViewModel
    private lateinit var newsBinding: FragmentNewsBinding
    private lateinit var adapter: NewsViewPage2Adapter
    private var type: Int = 0
    private var mList: MutableList<TypeInterf> = arrayListOf()
    //top(头条，默认),shehui(社会),guonei(国内),guoji(国际),yule(娱乐),tiyu(体育)junshi(军事),keji(科技),caijing(财经),shishang(时尚)
    private val mZhList = arrayOf("头条", "社会", "国内", "国际", "娱乐", "体育", "军事", "科技", "财经", "时尚")


    override fun initDataBind(inflater: LayoutInflater) {
        newsBinding = FragmentNewsBinding.inflate(inflater)
    }

    override fun initView(view: View?, savedInstanceState: Bundle?) {
        newsViewModel =
            ViewModelProviders.of(this).get(NewsViewModel::class.java)
//        setAdapter()
        for (s in mZhList) {
            newsBinding.tabLayout.addTab(newsBinding.tabLayout.newTab().setText(s))
        }

        newsBinding.viewPager2.setPageTransformer(DepthPageTransformer())
    }

    override fun initListener() {
        newsBinding.viewPager2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int {
                return mZhList.size
            }

            override fun createFragment(position: Int): Fragment {

                return NewsContentFragment(position)
            }
        }

        TabLayoutMediator(
            newsBinding.tabLayout,
            newsBinding.viewPager2,
            TabLayoutMediator.TabConfigurationStrategy { tab, position ->
                tab.text = mZhList[position]
            }).attach()


        /*    newsBinding.viewPager2.registerOnPageChangeCallback(object :
                ViewPager2.OnPageChangeCallback() {
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {
                    super.onPageScrolled(position, positionOffset, positionOffsetPixels)

                    println("onPageScrolled position...${position}")

                }

                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    type = position
                    println("onPageSelected position...${position}")
                    initData()
                }
            })*/
    }

    @SuppressLint("CheckResult")
    override fun initData() {


    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_news
    }

    override fun databing(): View {
        return newsBinding.root
    }

    private fun setAdapter() {
        /* newsBinding.viewPager2.adapter = context?.let {
             NewsViewPage2Adapter(
                 it,
                 R.layout.fragment_new_content,
                 mutableListOf(*mEnList)
             )
         }!!.apply {
             setOnItemClickListener(object : BaseRecyclerViewAdapter.OnItemClickListener {
                 override fun onItemClick(position: Int) {
                     Log.d(TAG, "onItemClick..." + position.toString())
                 }
             })

         }.apply {
             setOnItemLongClickListener(object :
                 BaseRecyclerViewAdapter.OnItemLongClickListener {
                 override fun onItemLongClick(position: Int): Boolean {
                     Log.d(TAG, "onItemLongClick...$position")

                     return true
                 }

             })
         }*/
    }
}
